var WxParse = require('../../wxParse/wxParse.js');
var app = getApp();
Page({
  data: {
    paynumber:"",
    payTypeNote:"",
    paydisable:false,
    remainTotal:"",
    shopname:"",
    shopid:"0",
  },
  onLoad: function (e) {
    var that = this;
    if (e && e.scene) {
      var scene = decodeURIComponent(e.scene).split('&');
      console.log(scene);
      var secneObj = {};
      for (var i = 0; i < scene.length; i++) {
        var arr = scene[i].split('=');
        var key = arr[0];
        secneObj[key] = arr[1];
      }
      e = secneObj;
    }
    if(e && e.sid){
      that.data.shopid = e.sid;
    }
  },
  onShow: function () {
    var that = this;
    if (!app.globalData.plumSession) {
      app.plumSessionCallback = plumSession => {
        that.requestpayType();
        that.getshopinfo(that.data.shopid);
      }
    }
    else{
      that.requestpayType();
      that.getshopinfo(that.data.shopid);
    }
  },
  getshopinfo:function(id){
    var that = this;
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_shop_detail',
        sid: that.data.shopid
      },
      success: function (res) {
        if (res.data.ec == 200 && res.data.data.name) {
          that.setData({
            shopname: res.data.data.name,
          })
        }
        else{
          wx.showModal({
            title: '',
            content: '门店不存在',
            showCancel: false,
            complete: (res) => {
              wx.switchTab({
                url: '/pages/index/index',
              })
            }
          })
        }
      }
    })
  },
  requestpayType: function () {
    var that = this;
    wx.showLoading({
      title: '正在加载',
      mask: true,
      time: 2000
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_pay_coin'
      },
      success: function (res) {
        wx.hideLoading();
        console.log(res.data);
        if (res.data.ec == 200) {
          var payWay = that.data.payWay;
          if (!payWay){
            payWay = res.data.data ? res.data.data[0].type : 1
          }
          that.setData({
            payType: res.data.data,
            payWay: payWay
          })
          that.payWaychange(payWay);
        } else {
          console.log(res.data)
          that.setData({
            payType: []
          })
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  dataChange: function (e) {
    var that = this;
    var curVal = e.detail.value;
    const regex = /^\d+$/; 
    if(!regex.test(curVal)){
      curVal = that.data.paynumber
    }
    var disable = false;
    for (let i = 0; i < that.data.payType.length; i++) {
      if (that.data.payWay == that.data.payType[i].type) {
        if(that.data.payType[i].balance<=curVal)
          disable=true;
      }
    }

    that.setData({
      paynumber: curVal,
      remainTotal : curVal,
      paydisable : disable,
    })
  },
  payRadioChange: function (e) {
    var that = this;
    var payWay = e.detail.value;
    that.payWaychange(payWay);
  },
  payWaychange: function (payWay){
    var that = this;
    var payType = that.data.payType ? that.data.payType:[];
    console.log(payType)
    var payTypeNote = '', balance = 0;
    var disable = false;
    for (let i = 0; i < payType.length; i++) {
      if (payWay == payType[i].type) {
        payTypeNote = payType[i].typeNote;
        if(payType[i].balance<=that.data.remainTotal)
          disable=true;
      }
    }

    this.setData({
      payWay: payWay,
      payTypeNote: payTypeNote,
      paydisable : disable,
    })
    //console.log(remainTotal + '--' + balance + '--'+payTypeNote + '--' + payWay);
  },
  orderPay: function (e) {
    var that = this;
    if(!that.data.payWay)return;
    if(!that.data.paynumber)return;
    wx.showModal({
      title: '',
      content: '确定使用'+that.data.payTypeNote+'(数量：'+that.data.paynumber+')'+'吗',
      complete: (res) => {
        if (res.cancel) {
          
        }
        if (res.confirm) {
          that.orderPay2();
        }
      }
    })
  },
  orderPay2 : function(){
    var that = this;
    var data = {};
    data.map = 'applet_coin_pay';
    data.payWay = that.data.payWay;
    data.paynumber = that.data.paynumber;
    data.shopid = that.data.shopid;
    wx.hideLoading();
    //发起请求，获取列表列表
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          wx.showModal({
            title: '提示',
            content: '支付成功',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                if(data.payWay=="11")
                  wx.redirectTo({
                    url: '/subpages1/meatcoin/meatcoin'
                  });
                if(data.payWay=="12")
                  wx.redirectTo({
                    url: '/subpages1/meatticket/meatticket'
                  });  
                if(data.payWay=="13")
                  wx.redirectTo({
                    url: '/subpages1/meatbacon/meatbacon'
                  });
                if(data.payWay=="14")
                  wx.redirectTo({
                    url: '/subpages1/meatpig/meatpig'
                  });
              }
            }
          });
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  getunionId: function (e) {
    var that = this;
    var userdata = e.detail;
    console.log(e.detail);
    app.getUnionid(that, userdata);
  },
})
// 根据数组某个字段排序
function sortValue(property,secprop) {
  return function (a, b) {
    var value1 = a[property];
    var value2 = b[property];
    var val = value2 - value1
    if (val==0){
      val = a[secprop] - b[secprop];
    }
    return val;
  }
}